﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using Xunit;
using System.Threading;

namespace NMigrate.Tests
{
	public class DirectoryTester : IDisposable
	{
		private string path;

		public DirectoryTester(string path)
		{
			if(path == null)
				path = Guid.NewGuid().ToString();
			this.path = path;
			if(System.IO.Directory.Exists(path))
				System.IO.Directory.Delete(path, true);
			System.IO.Directory.CreateDirectory(path);
		}

		public DirectoryTester()
			: this(null)
		{

		}

		public DirectoryInfo Directory
		{
			get
			{
				return new DirectoryInfo(path);
			}
		}

		#region IDisposable Members

		public void Dispose()
		{
			try
			{
				System.IO.Directory.Delete(path, true);
			}
			catch
			{
				Thread.Sleep(0);
				System.IO.Directory.Delete(path, true);
			}
		}

		#endregion

		[DebuggerHidden]
		internal void AssertFileExist(string file)
		{
			var foundFile = new DirectoryInfo(path).GetFiles().FirstOrDefault(f => f.Name == file);
			if(foundFile == null)
			{
				Assert.False(true, "file " + file + " should exist in the directory " + path);
			}
		}

		public string MakeFilePath(string filePath)
		{
			return Path.Combine(path, filePath);
		}
	}
}
